<template>
  <div id="app-container">
    <!--条件查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="card.cardName" placeholder="会员卡"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList(-1)">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <router-link :to="'/account/cardAdd/'">
        <el-button type="default" @click="resetData()" style="margin-left: 800px">添加会员卡</el-button>
      </router-link>
    </el-form>

    <!--数据显示列表-->
    <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="90">
        <template slot-scope="scope">
          {{pageSize*(currentPage-1)+scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="会员卡" width="200" ></el-table-column>
      <el-table-column prop="gymName" label="支持场馆 " width="200" ></el-table-column>
      <el-table-column prop="price" label="价格 " width="200" ></el-table-column>
      <el-table-column label="图片 " width="200" >
        <template slot-scope="scope">
          <img :src="' http://rj0387v46.hn-bkt.clouddn.com/'+scope.row.img" style="width: 200px">
        </template>
      </el-table-column>
      <el-table-column prop="account" label="类型" width="200" >
        <template slot-scope="scope">
          <span v-if="scope.row.type==0">次数卡</span>
          <span v-if="scope.row.type==1">储值类型</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <router-link :to="'/account/cardEdit/'+scope.row.id">
            <el-button type="success" size="mini" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([dataList[1], dataList[2]])">切换第二、第三行的选中状态</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
      <el-button @click="deleteAll()">删除所选</el-button>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 2, 3, 5]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import memberCard from "../../../api/memberCard";
import card from "../../../api/card";

export default {
  name: "card",
  data(){
    return{
      dataList:[],
      currentPage:1,
      pageSize:3,
      total:0,
      card:{},
    }
  },
  methods:{
    getList(row){
      if (row==-1){
        this.currentPage=1;
      }
      card.getList(this.currentPage,this.pageSize,this.card).then(res=>{
        this.dataList=res.data.data;
        this.total=res.data.total;
      })
    },
    resetData(){
      this.card={};
    },
    handleDelete(id){
      this.$confirm("是否确定删除数据？", "提示", {type: 'warning'})
        .then(() => {
          card.delete(id).then(res=>{
            this.$message.success(res.message);
            this.currentPage = (this.total-1)        % this.pageSize == 0 ? this.currentPage-1 : this.currentPage
            //当页数小于为0 就给它为1页 否则就还是当前页
            this.currentPage = this.currentPage < 1 ? 1 : this.currentPage
            this.getList();
          })
        })
        .catch(() => {
          // 表示点击了取消
          this.$message.info("取消删除操作");
        });
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.getList();
    },
    handleCurrentChange(currentPage){
      this.currentPage=currentPage;
      this.getList();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteAll(){
    }
  },
  mounted() {
    this.getList()
  }
}
</script>

<style scoped>

</style>
